package com.yzy.fly.mapper;

import com.yzy.fly.entity.role.FlyComponent;
import com.yzy.fly.entity.role.FlyProcessLine;
import com.yzy.fly.kernel.mapper.FlyBasicMapper;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.Collection;
import java.util.List;


@Mapper
public interface FlyProcessLineMapper extends FlyBasicMapper<FlyProcessLine> {
    @Select("select * from fly_process_line a where a.id = #{id}")
    FlyProcessLine selectById(@Param("id") int id);

    @Select("select * from fly_process_line a where 1=1")
    List<FlyProcessLine> selectAll();

    @Select("select * from fly_process_line a where 1=1 and a.processId = #{processId} ")
    List<FlyProcessLine> selectAllByProcessId(@Param("processId") int processId);


    @Select("select * from fly_process_line a where 1=1 and a.systemId = #{systemId}")
    List<FlyProcessLine> selectAllBySystemId(@Param("systemId") int systemId);

//    @Select({"select * from fly_process_line a where 1=1 and a.classId = (select b.fieldId from fly_field_annotation b where b.id = #{annotationId})",
//    "","",""})
    @Select({"<script>",
            "select * ",
            "from fly_process_line a",
            "where 1=1",
            "<when test='componentId!=null'>",
            "AND a.componentId = #{componentId}",
            "</when>",
            "<when test='systemId!=null'>",
            "AND a.systemId = #{systemId}",
            "</when>",
            "</script>"})
    List<FlyProcessLine> selectAllByParentId(@Param("componentId") Integer componentId, @Param("systemId") Integer systemId);

    @Select("select * from fly_process_line a where 1=1 and a.name = #{name}")
    FlyProcessLine selectOneByName(@Param("name") String name);

    @Delete("delete a from fly_process_line a where a.processId = #{processId}")
    int deleteByProcessId(@Param("processId") Integer processId);

    /**
     * 批量插入（mysql）
     * @param entityList
     * @return
     */
    Integer insertBatchSomeColumn(Collection<FlyProcessLine> entityList);

    @Select({"",
           "select * from fly_component a where a.id in",
           "( ",
           "   select d.businessId from fly_process_node d where 1=1 and d.id in",
           "   ( ",
           "      select c.`from` from fly_process_node b,fly_process_line c where 1=1 ",
           "      and b.businessId = #{javaId}",
           "      and b.id =  c.`to`",
           "   )",
           ")"
    })
    List<FlyComponent> selectAllFromJavaIdByToJavaId(@Param("javaId") int javaId);

    @Select({"",
            "select * from fly_component a where a.id in",
            "( ",
            "   select d.businessId from fly_process_node d where 1=1 and d.id in",
            "   ( ",
            "      select c.`from` from fly_process_node b,fly_process_line c where 1=1 ",
            "      and b.businessId = #{businessId}",
            "      and b.id =  c.`to`",
            "      and b.businessType =  #{businessType}",
            "   )",
            "   and d.businessIdType =  #{businessType}",
            ")"
    })
    List<FlyComponent> selectAllFromBusinessIdByToBusinessId(@Param("businessType") int type,@Param("businessId") int javaId);


}